﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace E.Y.E.Jobs
{
    class CommandClear : ICommand
    {
        private const string MESSAGE_COMMAND_TITLE = "/=======CLEAR COMMAND=======\\";
        private const string MESSAGE_SUCCESSFUL_CLEAR = "All Task is deleted from your task list.";
        private const string MESSAGE_UNDO_CLEAR = "Undo: All removed contents are restore.";
        private const string ERROR_MESSAGE_FAIL_CLEARING = "So embarrasing. Error occured while we are trying to clear your task list.";

        ArrayList OriginalDatabase;
        String Message;
        bool ErrorFlag;

        public CommandClear()
        {
            OriginalDatabase = new ArrayList();
            Message = "";
            ErrorFlag = true;
        }

        public bool Execute(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            try
            {
                OriginalDatabase = taskDatabase.GetCurrentList();
                taskDatabase.Clear();
                actionsHistory.AddNewHistory(this);
                Message = MESSAGE_SUCCESSFUL_CLEAR;
                ErrorFlag = false;
                return true;
            }
            catch
            {
                Message = ERROR_MESSAGE_FAIL_CLEARING;
                ErrorFlag = true;
                return false;
            }
        }

        public bool Undo(ref TaskManager taskDatabase, ref CommandTypeManager CommandTypeDatabase, ref History actionsHistory)
        {
            try
            {
                bool UndoSuccess = taskDatabase.RestoreList(OriginalDatabase);
                Message = MESSAGE_UNDO_CLEAR;
                ErrorFlag = false;
                return UndoSuccess;
            }
            catch
            {
                return false;
            }
        }

        public string getCommandEventTitle()
        {
            return MESSAGE_COMMAND_TITLE;
        }

        public void setMessage(string desiredMessage)
        {
            Message = desiredMessage;
        }

        public string getMessage()
        {
            return Message;
        }

        public void SetError(bool desiredBool)
        {
            ErrorFlag = desiredBool;
        }

        public bool GetErrorState()
        {
            return ErrorFlag;
        }

        public InformationPackage ConvertToInfoPackage()
        {
            InformationPackage Result = new InformationPackage();

            if (ErrorFlag)
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.OperationError;
            }
            else
            {
                Result.CommandType = InformationPackageEnumClass.CommandType.Clear;
            }
            Result.Message = Message;

            return Result;
        }
    }
}
